﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class PrinterQueueObject
    {
        public static PrinterQueue[] GetPrinterQueues()
        {
            return Query(null);
        }

        public static PrinterQueue GetPrinterQueue(int id)
        {
            PrinterQueue printerQueue = new PrinterQueue();

            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(PrinterQueueDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = PrinterQueueDesc.TableName;
            string sql = builder.QueryCommandLine;

            DataTable printerQueues = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            printerQueue.Id          = Convert.ToInt32(printerQueues.Rows[0][PrinterQueueDesc.Id]);
            printerQueue.Name        = Convert.ToString(printerQueues.Rows[0][PrinterQueueDesc.Name]);

            return printerQueue;
        }

        public static PrinterQueue GetPrinterQueue(string name)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(PrinterQueueDesc.Name, ConditionExpress.EQUALS, name);

            PrinterQueue[] set = Query(builder.ConditionString);

            if (set != null && set.Length > 0)
            {
                return set[0];
            }

            return null;
        }

        public static PrinterQueue[] Query(string condition)
        {

            // query factories from database
            string sql = "select * from " + PrinterQueueDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where " + condition;
            }
            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);


            // rebuild the data
            if (table != null && table.Rows.Count > 0)
            {
                PrinterQueue[] printerQueues = new PrinterQueue[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    PrinterQueue printerQueue = new PrinterQueue();

                    printerQueue.Id          = Convert.ToInt32(table.Rows[i][PrinterQueueDesc.Id]);
                    printerQueue.Name        = Convert.ToString(table.Rows[i][PrinterQueueDesc.Name]);

                    printerQueues.SetValue(printerQueue, i);
                }

                return printerQueues;
            }
            return null;
        }

        public static Int32 Add(PrinterQueue printerQueue)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(PrinterQueueDesc.Name,            printerQueue.Name);

            builder.QueryTableName = PrinterQueueDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(PrinterQueueDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = PrinterQueueDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(PrinterQueue printerQueue)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(PrinterQueueDesc.Name,           printerQueue.Name);

            builder.ConditionAdd(PrinterQueueDesc.Id, ConditionExpress.EQUALS, printerQueue.Id);
            builder.QueryTableName = PrinterQueueDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }
    }
}
